var globalCheckValue = null;

function getListData(tableData){
	if(typeof(tableData)=='undefined'){
		mediaAlert(null,'组件上方法getListData得加下this参数，如：getListData(this)');
		return false;
	}
	var url = tableData.url;
	var propertys = tableData.propertys;
	var hrefs = tableData.hrefs;
	var links = tableData.links;
	var css = tableData.css;
	var title = tableData.title;
	var checkBox = tableData.checkBox;
	var radio = tableData.radio;
	var hidden = tableData.hidden;
	var values = tableData.values;
	var opers = tableData.opers;
	var events = tableData.events;
	var form=tableData.form;
	var searchNum = tableData.searchNum;
	var maxLength = tableData.maxLength;
	var selff=tableData.selff;
	var radioValue = tableData.radioValue; //用于单选框的值回显，实例tableData.radioValue = window.parent.$("#workerId").val(); 将要勾选的值赋予radioValue。
	var checkBoxValue = tableData.checkBoxValue; //用于多选框的回显，实例tableData.checkBoxValue = window.parent.$("#workerId").val();将要勾选的值赋予checkBoxValue。值可以单值也可以逗号隔开。
	var checkBoxName = tableData.checkBoxName;
	var trBgColor = tableData.trBgColor; //某个字段的值，改变列表行的背景颜色，格式trBgColor : ["tr-bgcolor", "REJECT_STATE=已驳回"] 第一个参数是样式，第二个参数列值等于某值的时候。
	var doInput = tableData.doInput;// 列表实现列可编辑，第一个参数是可编辑input框的id命名，第二个参数就是指定那一列可编辑（可多个），实例doInput : ["REC_ID", "REAL_GIVE", ......]。
	var globalCheck = tableData.globalCheck;
	if(typeof(searchNum)=='undefined'){
		searchNum = "";
	}
	if(typeof(form)=='undefined'){
		form="form1";
	}
	setCommonHH(form);
	
	if(opers==null)opers=true;
	
	var checkBoxValues = "";
	var checkBoxNames = "";
	if(checkBoxValue != null && checkBoxValue != ""){
		checkBoxValues = checkBoxValue.split(",");
	}
	
	if(checkBoxName != null && checkBoxName != ""){
		checkBoxNames = checkBoxName.split(",");
	}
	
	if(globalCheck != null){
		globalCheckValue = globalCheck;
		if(checkBoxValues != ""){
			var arrayOne = globalCheck[0][1];
			for(var i = 0; i < checkBoxValues.length; i++){
				var flagOne = true;
				for(var j = 0; j < arrayOne.length; j++){
					if(checkBoxValues[i] == arrayOne[j]){
						flagOne = false;
					}
				}
				if(flagOne){
					globalCheck[0][1].push(checkBoxValues[i]);
					globalCheck[1][1].push(checkBoxNames[i]);
				}
			}
		}
		
	}
	showLoading();
	$.ajax({
		url: url, 
		data: $('#'+form).serialize(),
		type: "POST", 
		cache : false, 
		dataType: "json", 
		error: function(request) {
			closeLoading();
		    alert("表单提交出错，请稍候再试");
		},
		success: function(data) { 
			if(data.jobnos != undefined){
				jobnos = data.jobnos;
			}
			
			//checkAll
			//searchNum
			if($("#checkAll"+searchNum).length>0){
				 $("#checkAll"+searchNum).prop("checked",false); 
			}
			
			var ntable = null;
			if($("#"+form).find(".tableData").length==0){
				ntable = $("#ntable").find("table:last");
//				if(ntable.length == 0){
//					ntable = $("#ntable").find("table");
//				}
			}else{
				ntable= $("#"+form).find("#ntable").find("table:last");
//				if(ntable.length == 0){
//					ntable = $("#"+form).find("#ntable").find("table");
//				}
			}
		
		/*	ntable.find("table").find("tr:gt(0)").remove();*/
	        ntable.find("tbody").find("tr:gt(0)").remove();
		    var html = "";
		    html = "<tr>";
			html += "<td colspan='"+$("th").length+"'><center>数据记录暂时不存在</center></td>";
			html += "</tr>";
		/*	ntable.find("table").find("tr").last().after(html);*/
			ntable.find("tbody").find("tr").last().after(html);
		    var dataObj = data.list; 
		    var pageJson = data.params;
		    //分页
		    if($("#"+form).find(".tableData").length==0){
		    	 $(".tableData").html(writePage1(pageJson));
		    }else{ 
		    	 $("#"+form).find(".tableData").html(writePage1(pageJson,searchNum));
		    }
		   
		    $("#go").keyup(function(e){
				this.value=this.value.replace(/\D/g,'');
			 });
		    if(dataObj.length>0){ 
		    	 //分页
			    if($("#"+form).find(".tableData").length==0){
			    	 $(".tableData").html(writePage(pageJson));
			    }else{ 
			    	 $("#"+form).find(".tableData").html(writePage(pageJson,searchNum));
			    }
			   
			    $("#go").keyup(function(e){
					this.value=this.value.replace(/\D/g,'');
				 });
		    	/*ntable.find("table").find("tr:gt(0)").remove();*/
			    ntable.find("tbody").find("tr:gt(0)").remove();
				var nn = 0;
				$.each(dataObj,function(i){
					nn++;
					//添加表格数据行
					var eventsStr = "";
					if(typeof(events)!='undefined'&&events!=null){
						for(var e = 0;e<events.length;e++){
							var temp = "";							
							var t = events[e].substring(events[e].indexOf("(")+1,events[e].indexOf(")"));//REC_ID,TASKPACKAGE_NAME
							var s = t.split(",");
							for(var m = 0;m<s.length;m++){
								if(m!=0){ 
									temp+=","; 
								} 
								temp+="'"+dataObj[i][s[m]]+"'";
							}	
							eventsStr += " "+events[e].replace(t, temp)+" ";
						}
					}
					var trs = "<tr "+eventsStr+">";
					
						if(trBgColor != null){
							var trBgColors = trBgColor[1].split("=");
							if(trBgColors.length > 1){
								if(dataObj[i][trBgColors[0]] == trBgColors[1]){
									trs = "<tr "+eventsStr+" class=\"" + trBgColor[0] + "\">";
								}
							}
						}
					    
					    //alert(hiddenStr)
						if(typeof(radio)!='undefined'&&radio!=null&&radio[1]==true){
							var idname="REC_ID";
							if(typeof(radio)!='undefined'){
								idname=radio[0]; 
							}
							if(radioValue == dataObj[i][idname]){
								trs+=	"<td class=\"smart-tb_checkbox\"><input type=\"radio\" id=\"checkId"+searchNum+"\" name=\"checkId"+searchNum+"\" "+getHiddens(hidden,dataObj[i])+" value=\""+replaceHTML(dataObj[i][idname])+"\" checked /></td>";
							}else{
								trs+=	"<td class=\"smart-tb_checkbox\"><input type=\"radio\" id=\"checkId"+searchNum+"\" name=\"checkId"+searchNum+"\" "+getHiddens(hidden,dataObj[i])+" value=\""+replaceHTML(dataObj[i][idname])+"\" /></td>";
							}
						}else if(!(typeof(checkBox)!='undefined'&&checkBox!=null&&checkBox[1]==false)){
							var idname="REC_ID";
							var checkFlag = true;
							var checkFlagOne = true;
							if(typeof(checkBox)!='undefined'){
								idname=checkBox[0];
							}
							
							for(var j = 0; j < checkBoxValues.length; j++){
								if(checkBoxValues[j] == dataObj[i][idname]){
									trs+=	"<td class=\"smart-tb_checkbox\"><input type=\"checkbox\" id=\"checkId"+dataObj[i][idname]+"\" name=\"checkId"+searchNum+"\" "+getHiddens(hidden,dataObj[i])+" value=\""+replaceHTML(dataObj[i][idname])+"\" checked ";
									if(globalCheck != null){
										trs+= "onclick=\"isCheckValue('checkId" + dataObj[i][idname] + "');\"";
									}
									trs+= "/></td>";
									checkFlag = false;
								}
							}
							
							if(checkFlag){
								if(globalCheck != null && globalCheck[0][1] != null && globalCheck[0][1] != []){
									var array = globalCheck[0][1];
									for(var j = 0; j < array.length; j++){
										if(array[j] == dataObj[i][idname]){
											trs+=	"<td class=\"smart-tb_checkbox\"><input type=\"checkbox\" id=\"checkId"+dataObj[i][idname]+"\" name=\"checkId"+searchNum+"\" "+getHiddens(hidden,dataObj[i])+" value=\""+replaceHTML(dataObj[i][idname])+"\" checked ";
											if(globalCheck != null){
												trs+= "onclick=\"isCheckValue('checkId" + dataObj[i][idname] + "');\"";
											}
											trs+= "/></td>";
											checkFlagOne = false;
										}
									}
								}
							}
							
							
							if(checkFlag && checkFlagOne){
								trs+=	"<td class=\"smart-tb_checkbox\"><input type=\"checkbox\" id=\"checkId"+dataObj[i][idname]+"\" name=\"checkId"+searchNum+"\" "+getHiddens(hidden,dataObj[i])+" value=\""+replaceHTML(dataObj[i][idname])+"\" ";
								if(globalCheck != null){
									trs+= "onclick=\"isCheckValue('checkId" + dataObj[i][idname] + "');\"";
								}
								trs+= "/></td>";
							}
							
						}
					   if(propertys!=null){
						   for(var k=0;k<propertys.length;k++){
							   var temp = dataObj[i][propertys[k]];
							   temp = getNewValues(values,propertys[k],temp,dataObj[i]);
							   if(typeof(temp)=='undefined')temp="";
							   var temp1 = temp;
							   var temp2 = temp;
							   var temp3 = temp;
							   if(typeof(title)!='undefined'&&title!=null){
								   if(title[k] !=null && title[k] !=""){
									   temp3=title[k];
								   }
							   }
							   if(typeof(links)!='undefined'&&links!=null){
								   for(var l=0;l<links.length;l++){
									   var m = links[l][0];
									   var p = links[l][1];
									   var o = links[l][2];	
									   var s = links[l][3];
									   if(o==k){
										   //判断是否有链接
										   if(s!=null){
											    var isView = isLinkView(s,dataObj[i]);
											   	if(isView=="true"){
												   if(m.indexOf("function")==0){
													   var methodName = m.split("=")[1];
													   var params = getHrefParams0(p,dataObj[i]);
													   var st = "";
													   if(typeof(css)!='undefined'&&css!=null){
														   st = getDataCss(css[k],dataObj[i]);
													   }
													   temp1="<a href=\"javascript:void(0);\" "+st+" onclick=\""+methodName+"("+params+")\">"+temp+"</a>";
												   }
											   	}
										   }else{
											   	if(true){
												   if(m.indexOf("function")==0){
													   var methodName = m.split("=")[1];
													   var params = getHrefParams0(p,dataObj[i]);
													   var st = "";
													   if(typeof(css)!='undefined'&&css!=null){
														   st = getDataCss(css[k],dataObj[i]);
													   }
													   temp1="<a href=\"javascript:void(0);\" "+st+" onclick=\""+methodName+"("+params+")\">"+temp+"</a>";
												   }
											   	}
										   }										
									   }										   	
								   }								   
							   }			
							   if(typeof(maxLength)!='undefined'){
								   for(var f=0;f<maxLength.length;f++){
									   var st = maxLength[f];
									   if(st!=null){
										   var sts = st.split("=");
										   if(sts[0]==propertys[k]){											   
											   var v = dataObj[i][sts[0]];
											   var len = parseInt(sts[1]);
											   if(v!=null&&v.length>=len){
												   temp1 = v.substring(0,len)+"...";
											   }
										   }
									   }									   									  
								   }
							   }
							   
							   var doInputFlag = false;
							   if(doInput!=null&&doInput.length > 1){
								   for(var p = 0; p < doInput.length; p ++){
									   if(propertys[k] == doInput[p]){
										   doInputFlag = true;
									   }
								   }
							   }
							   
							   if(css!=null){   
								   if(doInputFlag){
									   trs+="<td title=\""+temp3+"\" "+getDataCss(css[k],dataObj[i])+"><input type=\"text\" class=\"smart-focus_show\" id=\"" + propertys[k] + dataObj[i][doInput[0]] + "\" value=\"" + temp2 + "\"/></td>";
								   }else{
									   trs+="<td title=\""+temp3+"\" "+getDataCss(css[k],dataObj[i])+">"+temp1+"</td>";
								   }
							   }else{ 
								   if(doInputFlag){
									   trs+="<td title=\""+temp3+"\"><input type=\"text\" class=\"smart-focus_show\" id=\"" + propertys[k] + dataObj[i][doInput[0]] + "\" value=\"" + temp2 + "\"/></td>";
								   }else{
									   trs+="<td title=\""+temp3+"\">"+temp1+"</td>"; 
								   }
							   }
						   }
					   } 
					   if(opers==true){
						   trs+="<td>";
						   if(hrefs!=null){
							   for(var k=0;k<hrefs.length;k++){
								   var isView = isHrefView(hrefs[k][3],dataObj[i]);
								   if(isView=="true"){
									   //判断是否是调用函数
									   if(hrefs[k][0].indexOf("function")==0){
										   var methodName = hrefs[k][0].split("=")[1];
										   var params = getHrefParams0(hrefs[k][1],dataObj[i]);
										   trs+=	"<a href=\"javascript:void(0);\" style=\"color:red;\" onclick=\""+methodName+"("+params+")\">"+hrefs[k][2]+"</a>";
									   }else{//text-decoration:underline;
										   var params = getHrefParams1(hrefs[k][1],dataObj[i]);
										   trs+=	"<a href=\"javascript:void(0);\" style=\"color:red;\" onclick=\"onHrefs('"+ctp+hrefs[k][0]+params+"')\">"+hrefs[k][2]+"</a>";
									   }								   
								   }
								   if(isView != "true" && isView != "false"){
									   trs+= isView;
								   }
							   }
						   }						
							trs+="</td>";
					   }					 
							trs+= "</tr>";
						
							ntable.find("tbody").find("tr").last().after(trs);
				});
				//判断
				/*var trt = "<tr class=\"clear\"><td  class=\"clear\" colspan=\""+(propertys.length+2)+"\"></td></tr>";
				for(var i=0;i<pageJson.ps-nn;i++){ 
					ntable.find("table").find("tr").last().after(trt);
					ntable.find("table").find("tbody").find("tr").last().after(trt);
				}*/
			}else{
				/*var trt = "<tr class=\"clear\"><td  class=\"clear\" colspan=\""+(propertys.length+2)+"\"></td></tr>";
				for(var i=0;i<pageJson.ps-1;i++){ 
					ntable.find("table").find("tbody").find("tr").last().after(trt);
				}*/
			}
		  closeLoading();
		  
		   if(typeof(selff)!='undefined'){
				selff(data);
		   }		
		 	
	    }
		
		
	});
	  
	//alert(searchNum)	
	//setTableCss();
	eval("changeFk('"+searchNum+"')");	
}
function onHrefs(path){
	location.href=path;
}
function getHrefParams0(hrefs,json){
	var params = "";
	var j=0;
	if(hrefs!=null){
		var s = hrefs.split(",");
		if(s!=null){
			for(var i=0;i<s.length;i++){
				var p = s[i].split("=");
				if(p!=null){
					if(j++!=0){
						params+=",";
					}
					if(typeof(json[p[1]])=='undefined'||json[p[1]]==''||json[p[1]]=='undefined'){
						params+="'"+p[1]+"'";
					}else{
						params+="'"+json[p[1]]+"'";
					}
				}
			}
		}
	}
	return params;
}
function getHrefParams1(hrefs,json){
	var params = "";
	var n = 0;
	if(hrefs!=null){
		params+="?";
		var s = hrefs.split(",");
		if(s!=null){
			for(var i=0;i<s.length;i++){
				var p = s[i].split("=");
				if(p!=null){
						if(n++!=0){
							params+="&";
						}
						if(typeof(json[p[1]])=='undefined'||json[p[1]]==''||json[p[1]]=='undefined'){
							params+=s[i];
						}else{
							params+=p[0]+"="+json[p[1]];
						}						
				}
			}
		}
	}
	return params;
}
function isLinkView(links,json){
	if(links!=null){
		var s = links.split("]");
		if(s.length>1){
			s[0]=s[0].substring(1,s[0].length);
			s[1]=s[1].substring(1,s[1].length);
			if(typeof(json[s[0]])!='undefined'&&json[s[0]]!=null&&json[s[0]]!=''){
				if(s[1]!=null){
					var ss=s[1].split(",");
					if(ss!=null){
						for(var j=0;j<ss.length;j++){
							var st=ss[j].split("=");
							if(st[0]==json[s[0]]){
								return st[1];
							}	
						}
					}
				}
			}
		}
	}
	return "true";
}
function isHrefView(hrefs,json){
	if(hrefs!=null){
		var s = hrefs.split("]");
		if(s.length>1){
			s[0]=s[0].substring(1,s[0].length);
			s[1]=s[1].substring(1,s[1].length);
		//	if(typeof(json[s[0]])!='undefined'&&json[s[0]]!=null&&json[s[0]]!=''){
			if(typeof(s[0])!='undefined'&&s[0]!=null&&s[0]!=''){
				if(s[1]!=null){
					var ss=s[1].split(",");
				    var s1=s[0].split(",");
					if(ss!=null&&s1!=null){
	                    for(var i=0;i<s1.length;i++){
	                    	for(var j=0;j<ss.length;j++){
								var st=ss[j].split("=");
								if(st[0]==json[s1[i]]){
									return st[1];
								}	
							}
	                    }
					}
				}
			}
		}
	}
	return "true";
}
function getDataCss(css,json){
	//"[PROCESS_STATUS][1=color:red;2=color:green;]"
	if(css!=null){
		var s = css.split("]");
		if(s.length>1){
			s[0]=s[0].substring(1,s[0].length);
			s[1]=s[1].substring(1,s[1].length-1);
			if(typeof(json[s[0]])!='undefined'&&json[s[0]]!=null&&json[s[0]]!=''){
				if(s[1]!=null){
					var ss=s[1].split(",");
					if(ss!=null){
						for(var j=0;j<ss.length;j++){
							var st=ss[j].split("=");
							if(st[0]==json[s[0]]){
								return "style=\""+st[1]+"\"";
							}							
						}
					}
				}
			}else{
				return "";
			}
		}else{
			if(css!=null){
				return "style=\""+s[0]+"\"";
			}
		}
	}
	/*if(css!=null){
		var s = css.split("=");
		if(s.length>1){
			if(s[0]==v){
				return "style=\""+s[1]+"\"";
			}
		}else{
			if(css!=null){
				return "style=\""+s[0]+"\"";
			}
		}
	}*/
	return "";
}
function getHidden(id){
	var recIds = id.split(",");
	var s = new Array();
	if(recIds!=null){
		for(var i=0;i<recIds.length;i++){
			s.push($("#"+tableData.hidden+recIds[i]).val());
		}
	}
	return s ;
}
function getNewValues(values,name,value,json){
  if(values!=null){
	  for(var i=0;i<values.length;i++){
		  var s = values[i];
		  var ss = s.split("=");
		  if(ss[0]==name){
			  var ss1 = ss[1];
			  var ms =ss1;
			  var ovs = "";
			  if(ss1.indexOf("[")>0){
				 var ov = "";
				 var st = ss1.split("[");
				 ms=st[0];
				 ov=st[1];
				 ov = ov.replace("]","");
				 var ts = ov.split(",");
				 for(var k=0;k<ts.length;k++){
					 if(k!=0){
						 ovs+=",";
					 }
					 ovs+="'"+json[ts[k]]+"'";
				 }
			  }
			  if(ovs!=""){
				  ovs = ","+ovs;
			  }
			  var  func=eval(ms+"('"+value+"'"+ovs+")");	
			  return func;
		  }
	  }
  }
  return value;	
}
function getHiddens(hidden,json){
	var hiddenStr = "";
    if(typeof(hidden)!='undefined'&&hidden!=null&&hidden!=''){
    	for(var h = 0;h<hidden.length;h++){
    		var s = hidden[h].split("=");
    		hiddenStr+=" "+s[0]+"="+json[s[1]]+" ";
    	}
    }
    return hiddenStr;
}
var ct = true;
var h1 = 0;
var h2 = 0;
function setCommonHH(form){
	var hh = WINDOW_HEIGHT==0?$(window).height():WINDOW_HEIGHT;  
	if(ct){
		if($("#schDiv").length>0){
			h1=$("#schDiv").height();
		}
		if($("#oper_div").length>0){
			h2=$("#oper_div").height();
		}
		ct = false;
	}
	hh = hh-45-55-100-100;
	//alert(hh);
	setTagAndValue(form,"hh",hh); 
}
function showLoading(loading){
	var loadDivId="loadDivId";
	if(typeof(loading)=='undefined'){
		loading = "正在加载中...";
	}
	if($("#"+loadDivId).length==0){
		var loadDiv = "<div class=\"loadAll\" style=\"z-index:99999999\"></div><div id=\""+loadDivId+"\" class=\"loadDiv\"><div class=\"loadBox\"><Div class=\"loadBg\"></Div><div class=\"loadMain\"><em></em><p>"+loading+"</p></div></div></div>";
		$("body").append(loadDiv); 
	}else{
		$("#"+loadDivId).css("display","block");   
	}	
}

function closeLoading(){
	var loadDivId="loadDivId";
	$(".loadAll").remove(); 
	$("#loadDivId").remove();
};


function getPaging(pageJson,dataObj){
	  var searchNum = "";
	    if($(".tableData").length==0){
	    	 $(".tableData").html(writePage1(pageJson));
	    }else{ 
	    	$(".tableData").html(writePage1(pageJson,searchNum));
	    }
	   
	    $("#go").keyup(function(e){
			this.value=this.value.replace(/\D/g,'');
		 });
	    if(dataObj.length>0){ 
		    if($(".tableData").length==0){
		    	 $(".tableData").html(writePage2(pageJson));
		    }else{ 
		    	$(".tableData").html(writePage2(pageJson,searchNum));
		    }
		   
		    $("#go").keyup(function(e){
				this.value=this.value.replace(/\D/g,'');
			 });		
		}
}


function getPaging2(pageJson,dataObj,tab,Num){
	  var searchNum = Num;
	  var tableData = ".tableData"+tab;
	    if($(tableData).length==0){
	    	 $(tableData).html(writePage1(pageJson));
	    }else{ 
	    	$(tableData).html(writePage1(pageJson,searchNum));
	    }
	   
	    $("#go").keyup(function(e){
			this.value=this.value.replace(/\D/g,'');
		 });
	    if(dataObj.length>0){ 
		    if($(tableData).length==0){
		    	 $(tableData).html(writePage(pageJson));
		    }else{ 
		    	$(tableData).html(writePage(pageJson,searchNum));
		    }
		   
		    $("#go").keyup(function(e){
				this.value=this.value.replace(/\D/g,'');
			 });		
		}
}


function isCheckValue(checkBoxId){
	var flag = true;
	if ($("#" + checkBoxId).prop("checked")) {
		var array = globalCheckValue[0][1];
		for(var j = 0; j < array.length; j++){
			if(array[j] == $("#" + checkBoxId).val()){
				flag = false;
				break;
			}
		}
		if(flag){
			globalCheckValue[0][1].push($("#" + checkBoxId).val());
			if(globalCheckValue.length > 1){
				for(var i = 1; i < globalCheckValue.length; i++){
					globalCheckValue[i][1].push($("#" + checkBoxId).attr(globalCheckValue[i][0]));
				}
			}
		}
	}else{
		var array = globalCheckValue[0][1];
		for(var j = 0; j < array.length; j++){
			if (array[j] == $("#" + checkBoxId).val()){
				globalCheckValue[0][1].splice(j, 1); 
				if(globalCheckValue.length > 1){
					for(var i = 1; i < globalCheckValue.length; i++){
						globalCheckValue[i][1].splice(j, 1); 
					}
				}
			}
		}
	}
}